package music_store.ms.controller.backend;


import music_store.ms.common.Const;
import music_store.ms.common.ResponseCode;
import music_store.ms.common.ServerResponse;
import music_store.ms.pojo.Music;
import music_store.ms.pojo.User;
import music_store.ms.service.IMusicService;
import music_store.ms.service.impl.MusicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/manage/music/")
public class MusicManageController {

    @Autowired
    private IMusicService iMusicService;

    //添加音乐、更新音乐二合一
    @PostMapping("save_music.do")
    public ServerResponse addOrUpdateMusic(HttpSession session, Music music){
        if (checkAdminRole(session).isSuccess()){
            return iMusicService.addOrUpdateMusic(music);
        }
        return ServerResponse.createByErrorMessage("不是管理员，无权限操作。");
    }

    @PostMapping("set_sale_status.do")
    public ServerResponse setSaleStatus(HttpSession session, Integer musicId , Integer status){
        if (checkAdminRole(session).isSuccess()){
            return iMusicService.setSaleStatus(musicId,status);
        }
        return ServerResponse.createByErrorMessage("不是管理员，无权限操作。");
    }

    @PostMapping("detail.do")
    public ServerResponse musicDetail (HttpSession session,Integer musicId){
        if (checkAdminRole(session).isSuccess()){
            return iMusicService.getMusicDetailById(musicId);
        }
        return ServerResponse.createByErrorMessage("不是管理员，无权限操作。");
    }

    @PostMapping("list.do")
    public ServerResponse getMusicList (HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum
            ,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        if (checkAdminRole(session).isSuccess()){
            return iMusicService.getMusicList(pageNum, pageSize);
        }
        return ServerResponse.createByErrorMessage("不是管理员，无权限操作。");
    }

    //搜索歌曲。--游客也可用
    @PostMapping("search_music_by_musicname.do")
    public ServerResponse musicSearchByMusicName(String musicName,
                                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return iMusicService.searchMusicByMusicName(musicName,pageNum,pageSize);
    }

    @PostMapping("search_music_by_artist.do")
    public ServerResponse musicSearchByArtist(String artist,
                                              @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return iMusicService.searchMusicByArtist(artist,pageNum,pageSize);
    }

    @PostMapping("search_music_by_albumname.do")
    public ServerResponse musicSearchByAlbumName(String albumName,
                                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return iMusicService.searchMusicByAlbumName(albumName,pageNum,pageSize);
    }

    //搜索歌曲以歌曲id。 --仅管理可用
    @PostMapping("search_music_by_musicid.do")
    public ServerResponse musicSearchByMusicId(HttpSession session, Integer musicId,
                                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        if (checkAdminRole(session).isSuccess()){
            return iMusicService.getMusicList(pageNum, pageSize);
        }
        return iMusicService.searchMusicByMusicId(musicId);
    }










    //检测当前用户是否为管理员的私有方法
    private ServerResponse checkAdminRole (HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (user.getRole() == Const.Role.ROLE_ADMIN){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorCodeMessage(1,"不是管理员。");
    }


}
